package cn.jee.jsp.filter;

import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
@WebFilter("/sec/*")//拦截的资源
public class SecFilter extends HttpFilter {

  @Override
  public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpSession session=req.getSession();
    Object obj=session.getAttribute("login_user");
    if(obj==null){
      res.sendRedirect(req.getContextPath()+"/login.jsp");
      return;
    }else {
      super.doFilter(req, res, chain);
    }
  }
}
